/*
Copyright (c) 2006 - 2008  Universitaet Osnabrueck, virtUOS
Authors: Markus Ketterl, Johannes Emden, Nils Schuhmacher, Andreas Rickling

This file is part of virtPresenter.

virtPresenter is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

virtPresenter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with virtPresenter.  If not, see <http://www.gnu.org/licenses/>.
*/

package virtPresenter.model
{	
	import flash.net.LocalConnection;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import virtPresenter.event.CairngormErrorEvent;
	import flash.events.SecurityErrorEvent;
	import virtPresenter.*;
	import mx.core.Application;
	
	[Bindable]
	/**
	 * Class creates a recieving and sending local connection to be able
	 * to communicate with the presentation SWF. 
	 */
	public class Flex2SWFConnector
	{	
		/**
		 * The sending local connection, which can send messages to the presentation SWF. 
		 */
		public var sendingLC:LocalConnection;
		/**
		 * The recieving local connection, which can recieve messages from the presentation SWF. 
		 */
		public var recievingLC:LocalConnection;
		public var secError:SecurityError;
		
		/**
		 * Constructor of the class. Should be Singelton.
		 * There should only be exactly 2 LocalConnection objects, that manage the 
		 * communication between flex and the SWF8 prsentation. Instantiates the
		 * two local connections and registers the Flex2SWFConnector as a client of the recieving LC.
		 */		
		public function Flex2SWFConnector()
		{	
			sendingLC=new LocalConnection();
			sendingLC.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
  			recievingLC = new LocalConnection();
			recievingLC.client = this;						
		}
		
		/**
		 * Connects the flex application with the presentation swf to be able
		 * to recieve messages from it.
		 */
		public function connectRecievingLC():void
		{			
			try
			{
				recievingLC.connect("swf2Flex"); 
			}
			catch(err:Error){
				trace("Flex2SWFConnector. Error in recievingLC!");
				CairngormEventDispatcher.getInstance().dispatchEvent(new CairngormErrorEvent(err,"Fehler in recievingLC. Evtl. läuft der virtPresenter bereits in Ihrem Browser. Bitte schließen Sie dann diese Version. Momentan ist es nur erlaubt eine Instanz des virtPresenter zur Zeit laufen zu lassen. Wir bitten um Entschuldigung für jegliche dadurch entstehende Unannehmlichkeiten!"));							
			}
		}
		
		/**
		 * Method to set the current slide number.
		 */
		//Can be deleted?
		public function setCurrentSlideNum(SlideNum:Number):void{
   			//var model : virtPresenterLocator = virtPresenterLocator.getInstance();   			
   		}
   		
   		private function securityErrorHandler(event:SecurityErrorEvent):void {
            trace("securityErrorHandler: " + event);            
        }		
		
	}
}